package cn.xwj.mongoDBtest;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
public class AppCollection
{
    public static void main( String[] args )
    {
       MongoDatabase mongoDatabase=MongoDBCon.MongoDBConInstence();
//       获取集合
        try {
           MongoCollection<Document> collection=mongoDatabase.getCollection("thinkjoy");
            System.out.println(collection.count());
            //插入文档
            /**
             * 1. 创建文档 org.bson.Document 参数为key-value的格式
             * 2. 创建文档集合List<Document>
             * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>)
             * 插入单个文档可以用 mongoCollection.insertOne(Document)
             * */
            Document document=new Document("thinkjoy","thinkjoy-job").append("lao","乌龟")
                    .append("xiao","狗").append("da","鳄鱼");
            List<Document> documents=new ArrayList<>();
            documents.add(document);
            collection.insertMany(documents);
            System.out.println("文档插入成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("文档插入失败");
        }
    }
}
